package org.swqsbssddqs.shortlink.admin.config;

import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.swqsbssddqs.shortlink.admin.common.biz.user.UserFlowRiskControlFilter;
import org.swqsbssddqs.shortlink.admin.common.biz.user.UserTransmitFilter;


@Configuration
@AllArgsConstructor
public class UserConfiguration {
    private final StringRedisTemplate stringRedisTemplate;
    private final UserFlowRiskConfiguration userFlowRiskConfiguration;
    @Bean
    public FilterRegistrationBean<UserTransmitFilter> getFilter() {
        FilterRegistrationBean<UserTransmitFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new UserTransmitFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(10);
        return registration;
    }

    @Bean
    @ConditionalOnProperty(name = "short-link.flow-limit.enable", havingValue = "true")
    public FilterRegistrationBean<UserFlowRiskControlFilter> userFlowRiskControlFilterFilterRegistrationBean(
            StringRedisTemplate stringRedisTemplate,
            UserFlowRiskConfiguration userFlowRiskConfiguration
    ) {
        FilterRegistrationBean<UserTransmitFilter> registration = new FilterRegistrationBean<>();
        FilterRegistrationBean<UserFlowRiskControlFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new UserFlowRiskControlFilter(stringRedisTemplate,userFlowRiskConfiguration));
        registration.addUrlPatterns("/*");
        registration.setOrder(100);

        return filterRegistrationBean;
    }

}
